public class CaseSolutionExt {
	
	private final Case c;
	
	public CaseSolutionExt(ApexPages.StandardController stdController) 
	{
    	this.c = (Case)stdController.getRecord();
    }
    
    String SolDesc;
    
    public string getSolDesc() {
        return SolDesc;
    }
            
    public PageReference refreshSolution() 
    {
        Solution[] s = [select id,solutionName,user_story__c from solution where id in (select solutionid from casesolution where caseid = :c.Id) Limit 1];
        
        Try
        {
	        if (s.size()>0)
	        {
	        	SolDesc = s[0].SolutionName;
	        	if (c.solution__c != s[0].id)
	        	{
	        		c.solution__c = s[0].id;
	        		If(s[0].user_story__c != null)If(c.isclosed==false)
	        		{
	        			c.status = 'User Story Submitted';
	        		}
	        		update c;
	        	}
	        }
	        else if(s.size()<1)
	        {
	        	if (c.solution__c != null)
	        	{
	        		c.solution__c = null;
	        		if(c.status == 'User Story Submitted')
	        		{
	        			c.status = 'Open';
	        		}
	        		update c;
	        	}
	        }
        }
        catch(exception e)
        {
        	system.debug('We were unable to update the record. There was probably a conflict with a validation rule.');
        }
        
        return null;
    }
}